﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace Joker
{
    class Z
    {
        public static DataSet QuerySql()   /*查询数据库并返回DataSet*/
        {
            SqlConnection SqlCon = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Joker;Integrated Security=True");
            SqlCon.Open();
            SqlCommand SqlOrd = new SqlCommand("select * from CompanyInfo" + Environment.NewLine + "select * from CompanyImage", SqlCon);
            SqlDataAdapter SqlDaAda = new SqlDataAdapter(SqlOrd);
            DataSet ComInfoItem = new DataSet();
            SqlDaAda.Fill(ComInfoItem);
            return ComInfoItem;
            SqlCon.Close();
        }
        public static string aa(string str)   /*查询数据库并返回DataSet*/
        {
            SqlConnection SqlCon = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Joker;Integrated Security=True");
            SqlCon.Open();
            SqlCommand SqlOrd = new SqlCommand("select * from CompanyInfo" + Environment.NewLine + "select * from CompanyImage", SqlCon);
            SqlDataAdapter SqlDaAda = new SqlDataAdapter(SqlOrd);
            DataSet ComInfoItem = new DataSet();
            SqlDaAda.Fill(ComInfoItem);
            //return ComInfoItem;
            str = ComInfoItem.Tables.Count.ToString();
            return str;
            SqlCon.Close();
        }
        public static void UpDateSql(DataTable DataTab)
        {
            SqlConnection SqlCon = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Joker;Integrated Security=True");
            SqlCon.Open();
            SqlCommand SqlOrd = new SqlCommand("select * from CompanyInfo", SqlCon);
            SqlDataAdapter SqlDaAda = new SqlDataAdapter(SqlOrd);
            SqlCommandBuilder SqlBuder = new SqlCommandBuilder(SqlDaAda);
            SqlDaAda.Update(DataTab);
        }
        public static void ShowSqlText(TextBox TName, TextBox TSex, TextBox TAdd, TextBox TCEO, TextBox TBank,TextBox TBankID, TextBox TLogyID)
        {
            TName.Text = QuerySql().Tables[0].Rows[0][1].ToString();
            TSex.Text = QuerySql().Tables[0].Rows[0][2].ToString();
            TAdd.Text = QuerySql().Tables[0].Rows[0][3].ToString();
            TCEO.Text = QuerySql().Tables[0].Rows[0][4].ToString();
            TBank.Text = QuerySql().Tables[0].Rows[0][5].ToString();
            TBankID.Text = QuerySql().Tables[0].Rows[0][6].ToString();
            TLogyID.Text = QuerySql().Tables[0].Rows[0][7].ToString();
        }
        //public static void SqlUpDate(String TabName, String ColName, String ConStr)   /*更新数据库*/
        //{
        //    JokerSqlCon("update " + TabName + " set " + ColName + "='" + ConStr + "'").ExecuteNonQuery();
        //}
        //public static object SqlQueryNo(String TabName, String ColName)   /*无条件查询数据库*/
        //{
        //return JokerSqlCon("select " + ColName + " from " + TabName).ExecuteScalar();
        //}
        //public static object SqlQueryCon(String TabName, String ColName, String ConCol, String ConStr)   /*有条件查询数据库*/
        //{
        //    return JokerSqlCon("select " + ColName + " from " + TabName + " where " + ConCol + "=" + "'" + ConStr + "'").ExecuteScalar();
        //}
        //public static DataTable SelSqlDataTable(String TabName)   /*查询数据库并返回DataTable*/
        //{
        //    SqlDataAdapter SqlDataAda = new SqlDataAdapter(JokerSqlCon("select * from " + TabName));
        //    DataTable DataTab = new DataTable();
        //    SqlDataAda.Fill(DataTab);
        //    return DataTab;
        //    DataTab.Clear();
        //}
        public static Image ShowSqlImage(ComboBox ImgName, String TabName)   /*从数据库提取Image并返回*/
        {
            Byte[] BytBuff = (Byte[])(QuerySql().Tables[1].Rows[ImgName.SelectedIndex][2]);
            MemoryStream MemSte = new MemoryStream(BytBuff);
            MemSte.Write(BytBuff, 0, BytBuff.Length);
            MemSte.Seek(0, SeekOrigin.Begin);
            Image RetIme = Image.FromStream(MemSte);
            MemSte.Close();
            return RetIme;
        }
        public static String OpenloadFile(String TxtName, String TypeName)   /*选择本地文件并返回路径*/
        {
            OpenFileDialog OpenFile = new OpenFileDialog();
            OpenFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            OpenFile.Multiselect = false;
            OpenFile.Title = TxtName;
            OpenFile.Filter = TypeName;
            if (OpenFile.ShowDialog() == DialogResult.OK)
            {
                return OpenFile.FileName;
            }
            else
            {
                return null;
            }
        }

        //public static void EditSingleData(TextBox Tx,Button EBt,String Tab,String Col,Button GBt)
        //{
        //    if (EBt.Text == "修改")
        //    {
        //        EBt.Text = "保存";
        //        Tx.ReadOnly = false;
        //        GBt.Visible = true;
        //    }
        //    else
        //    {
        //        SqlUpDate(Tab, Col, Tx.Text);
        //        GBt.Visible = false;
        //        Tx.ReadOnly = true;
        //        MessageBox.Show("成功");
        //        EBt.Text = "修改";
        //    }
        //}
        //public static void GiveUpEditSingle(TextBox Tx,Button EBt,Button GBt,TabPage Tab,Label Lab)
        //{
        //    Tx.ReadOnly = true;
        //    EBt.Text = "修改";
        //    GBt.Visible = false;
        //    Tx.Text = SelSqlDataTable(Tab.Name).Rows[0][Lab.Name].ToString();
        //}
        ////public static void ShowSqlText(TabPage Tab,TextBox TName,Label LName, TextBox TSex, Label LSex, TextBox TAdd, Label LAdd, TextBox TCEO, Label LCEO, TextBox TBank, Label LBank, 
        ////    TextBox TBankID, Label LBankID, TextBox TLogyID, Label LLogyID)
        ////{
        ////    DataTable DataTab = SelSqlDataTable(Tab.Name);
        ////    TName.Text = DataTab.Rows[0][LName.Name].ToString();
        ////    TSex.Text = DataTab.Rows[0][LSex.Name].ToString();
        ////    TAdd.Text = DataTab.Rows[0][LAdd.Name].ToString();
        ////    TCEO.Text = DataTab.Rows[0][LCEO.Name].ToString();
        ////    TBank.Text = DataTab.Rows[0][LBank.Name].ToString();
        ////    TBankID.Text = DataTab.Rows[0][LBankID.Name].ToString();
        ////    TLogyID.Text = DataTab.Rows[0][LLogyID.Name].ToString();
        ////}
        public static void EditTextBoxNatrue(TextBox ComTx, TextBox SexTx, TextBox AddTx, TextBox CEOTx, TextBox BankTx, TextBox BankIDTx, TextBox SociologyIDTx, bool Bo)
        {
            ComTx.ReadOnly = Bo;
            SexTx.ReadOnly = Bo;
            AddTx.ReadOnly = Bo;
            CEOTx.ReadOnly = Bo;
            BankTx.ReadOnly = Bo;
            BankIDTx.ReadOnly = Bo;
            SociologyIDTx.ReadOnly = Bo;
        }
        public static void EditButtonNatrue(Button ComBt, Button SexBt, Button AddBt, Button CEOBt, Button BankBt, Button BankIDBt, Button SociologyIDBt, String Tx)
        {
            ComBt.Text = Tx;
            SexBt.Text = Tx;
            AddBt.Text = Tx;
            CEOBt.Text = Tx;
            BankBt.Text = Tx;
            BankIDBt.Text = Tx;
            SociologyIDBt.Text = Tx;
        }
        public static void GiveUpButtonEdit(Button ComBt, Button SexBt, Button AddBt, Button CEOBt, Button BankBt, Button BankIDBt, Button SociologyIDBt, bool Bo)
        {
            ComBt.Visible = Bo;
            SexBt.Visible = Bo;
            AddBt.Visible = Bo;
            CEOBt.Visible = Bo;
            BankBt.Visible = Bo;
            BankIDBt.Visible = Bo;
            SociologyIDBt.Visible = Bo;
        }
        //public static void UpLoadImgSql(String TxtName, String TypeName)   /*上传本地文件到数据库*/
        //{
        //    OpenFileDialog OpenFile = new OpenFileDialog();
        //    OpenFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //    OpenFile.Multiselect = false;
        //    OpenFile.Title = TxtName;
        //    OpenFile.Filter = TypeName;
        //    if (OpenFile.ShowDialog() == DialogResult.OK)
        //    {
        //        FileStream FileStem = new FileStream(OpenFile.FileName, FileMode.Open, FileAccess.Read);
        //        BinaryReader TowV = new BinaryReader(FileStem);
        //        Byte[] BytImg = TowV.ReadBytes((int)FileStem.Length);
        //        SqlCommand SqlOrd = JokerSqlCon("insert into CompanyImage (ImageFile) values (@ImageFile)");
        //        SqlOrd.Parameters.Add("ImageFile", SqlDbType.Image, BytImg.Length).Value = BytImg;
        //        SqlOrd.ExecuteNonQuery();
        //        TowV.Close();
        //        FileStem.Close();
        //        MessageBox.Show("成功");
        //    }
        //    else
        //    {
        //        return;
        //    }
        //}
    }
}
